import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class XmmDialogUtils {
  /// 底部选择框
  /// {
  ///   title: 'title',
  ///   'onPressed': (){}
  /// }
  static void showSelectDialog(List menus) {
    showCupertinoModalPopup(
      context: Get.context!,
      builder: (context) {
        List<Widget> children = [];
        for (var item in menus) {
          children.add(CupertinoActionSheetAction(
            onPressed: () async {
              Navigator.pop(context);
              item['onPressed']?.call();
            },
            child: Text(item['title']),
          ));
        }
        return CupertinoActionSheet(
          actions: children,
          cancelButton: CupertinoActionSheetAction(
            onPressed: () {
              Navigator.pop(context);
            },
            child: const Text('Cancel'),
          ),
        );
      },
    );
  }

  static void showConfirmDialog(String title, Function() onConfirm) {
    showCupertinoDialog(
      context: Get.context!,
      builder: (context) {
        return CupertinoAlertDialog(
          content: Center(child: Text(title)),
          actions: <Widget>[
            CupertinoDialogAction(
              onPressed: () {
                Get.back();
                onConfirm.call();
              },
              child: const Text("Confirm"),
            ),
            CupertinoDialogAction(
              child: const Text("Cancel"),
              onPressed: () {
                Get.back();
              },
            ),
          ],
        );
      },
    );
  }

  /// 选择日期
  static Future<DateTime?> showDatePicker({
    required DateTime initialDateTime,
    DateTime? maximumDate,
    Function(DateTime value)? onDateTimeChanged,
  }) async {
    DateTime select = initialDateTime;
    return showCupertinoModalPopup(
      context: Get.context!,
      builder: (ctx) {
        return Container(
          height: 300,
          color: Colors.white,
          child: Column(
            children: [
              SizedBox(
                height: 55,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    CupertinoButton(
                      child: const Text('Cancel'),
                      onPressed: () {
                        Get.back();
                      },
                    ),
                    CupertinoButton(
                      child: const Text('Confirm'),
                      onPressed: () {
                        onDateTimeChanged?.call(select);
                        Get.back();
                      },
                    ),
                  ],
                ),
              ),
              Expanded(
                child: CupertinoDatePicker(
                  maximumDate: maximumDate,
                  initialDateTime: initialDateTime,
                  mode: CupertinoDatePickerMode.date,
                  onDateTimeChanged: (DateTime value) {
                    select = value;
                  },
                ),
              ),
            ],
          ),
        );
      },
    );
  }

}
